package com.funtl.framework.web.action.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;

import com.funtl.framework.java.abstracts.AbstractAction;
import com.funtl.framework.java.common.util.bean.DateUtil;
import com.funtl.framework.java.common.util.bean.StringUtil;
import com.funtl.framework.java.orm.hibernate.service.IHibernateDao;
import com.funtl.framework.web.entity.Admin;
import com.funtl.framework.web.entity.LogError;
import com.funtl.framework.web.entity.LogInfo;
import com.funtl.framework.web.entity.PurviewColumn;
import com.funtl.framework.web.entity.PurviewGroup;
import com.funtl.framework.web.entity.PurviewManager;
import com.funtl.framework.web.entity.PurviewMenu;
import com.funtl.framework.web.entity.User;
import com.funtl.framework.web.service.admin.IAdminService;
import com.funtl.framework.web.service.log.ILogInfoService;
import com.funtl.framework.web.service.purview.IPurviewColumnService;
import com.funtl.framework.web.service.purview.IPurviewManagerService;
import com.funtl.framework.web.service.purview.IPurviewMenuService;

/**
 * 管理员
 * @author WMLI
 * Create on 2013-5-14
 */
@Scope("prototype")
public class AdminAction extends AbstractAction {
	private static final long serialVersionUID = -5814689521973312164L;
	private JSONObject result;
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	private Admin admin;
	private IAdminService adminService;
	private IPurviewColumnService purviewColumnService;
	private IPurviewMenuService purviewMenuService;
	private IPurviewManagerService purviewManagerService;
	
	private ILogInfoService logInfoService;
	private IHibernateDao hibernateDao;
	
	//参数
    private String securityCode; 	//验证码
    private String rePwd;			//验证密码
	
	//page
    
    /**
     * 首页
     * @return
     * @throws Exception
     */
    public String admin_main() throws Exception {
    	//加载权限栏目
    	List<PurviewColumn> columns = purviewColumnService.getList(new PurviewColumn());
		List<PurviewMenu> menus = purviewMenuService.getList(new PurviewMenu());
		
		request.put("columns", columns);
		request.put("menus", menus);
    	
    	return SUCCESS;
    }
    
    /**
	 * 权限提示界面
	 * @return
	 * @throws Exception
	 */
	public String admin_purview() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 管理员管理界面
	 * @return
	 * @throws Exception
	 */
	public String admin_manager() throws Exception {
		//加载管理组
		List<PurviewManager> managers = purviewManagerService.getList(new PurviewManager());
		request.put("managers", managers);
		return SUCCESS;
	}
	
	/**
	 * 退出
	 * @return
	 * @throws Exception
	 */
	public String admin_loginOut() throws Exception {
		Admin user = (Admin) session.get("admin");
		logInfoService.insert(new LogInfo("管理员", "退出了系统", ServletActionContext.getRequest().getRemoteAddr(), user.getId(), user.getUserName(), DateUtil.getCurrentDate()));
		
		session.clear();
		return SUCCESS;
	}
	
	//action
    
    /**
     * 管理员登录
     * @return
     * @throws Exception
     */
    public String login() throws Exception {
    	String serverCode = (String) session.get("SESSION_SECURITY_CODE");
		if (!serverCode.equals(securityCode)) {
			jsonData.put("message", "验证码输入错误");
		} else {
			admin.setLoginPwd(StringUtil.toMD5(admin.getLoginPwd()));
			Admin user = adminService.login(admin);
			if (user == null) {
				jsonData.put("message", "用户名或密码错误");
			} else {
				List<String> actionMenus = purviewMenuService.queryActionMenu(user.getPurviewManagerId());
				session.put("admin", user);
				session.put("actionMenus", actionMenus);
				
				logInfoService.insert(new LogInfo("管理员", "登录了系统", ServletActionContext.getRequest().getRemoteAddr(), user.getId(), user.getUserName(), DateUtil.getCurrentDate()));
			}
		}
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
    }
    
    /**
     * 清除缓存
     * @return
     * @throws Exception
     */
    public String clear() throws Exception {
    	hibernateDao.clearCatch(Admin.class);
    	hibernateDao.clearCatch(LogError.class);
    	hibernateDao.clearCatch(LogInfo.class);
    	hibernateDao.clearCatch(PurviewColumn.class);
    	hibernateDao.clearCatch(PurviewGroup.class);
    	hibernateDao.clearCatch(PurviewManager.class);
    	hibernateDao.clearCatch(PurviewMenu.class);
    	hibernateDao.clearCatch(User.class);
    	
    	jsonData.put("message", "clear");
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
    }
	
	//common action
	
	@Override
	public String get() throws Exception {
		return null;
	}

	@Override
	public String query() throws Exception {
		List<Admin> list = adminService.getList(new Admin());
		jsonData.put("rows", list);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String insert() throws Exception {
		if (!admin.getLoginPwd().equals(rePwd)) {
			jsonData.put("message", "两次密码输入不一致,请重新输入");
		} else {
			Admin user = (Admin) session.get("admin");
			admin.setLoginPwd(StringUtil.toMD5(admin.getLoginPwd()));
			admin.setCreateDate(DateUtil.getCurrentDate());
			admin.setCreateId(user.getId());
			admin.setCreateName(user.getUserName());
			adminService.insert(admin);
		}
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		adminService.delete(admin);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String update() throws Exception {
		Admin user = (Admin) session.get("admin");
		admin.setUpdateDate(DateUtil.getCurrentDate());
		admin.setUpdateId(user.getId());
		admin.setUpdateName(user.getUserName());
		adminService.update(admin);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}
	
	//getter setter

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public IAdminService getAdminService() {
		return adminService;
	}

	@Resource
	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public String getSecurityCode() {
		return securityCode;
	}

	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}

	public IPurviewColumnService getPurviewColumnService() {
		return purviewColumnService;
	}

	@Resource
	public void setPurviewColumnService(IPurviewColumnService purviewColumnService) {
		this.purviewColumnService = purviewColumnService;
	}

	public IPurviewMenuService getPurviewMenuService() {
		return purviewMenuService;
	}

	@Resource
	public void setPurviewMenuService(IPurviewMenuService purviewMenuService) {
		this.purviewMenuService = purviewMenuService;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	@Resource
	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IPurviewManagerService getPurviewManagerService() {
		return purviewManagerService;
	}

	@Resource
	public void setPurviewManagerService(IPurviewManagerService purviewManagerService) {
		this.purviewManagerService = purviewManagerService;
	}

	public String getRePwd() {
		return rePwd;
	}

	public void setRePwd(String rePwd) {
		this.rePwd = rePwd;
	}

	public ILogInfoService getLogInfoService() {
		return logInfoService;
	}

	@Resource
	public void setLogInfoService(ILogInfoService logInfoService) {
		this.logInfoService = logInfoService;
	}
	
}
